package pay

import (
    "strconv"
	"dhfshop/models/member"
	"dhfshop/models/isv"
	"dhfshop/utils/math"
	"dhfshop/utils/helper"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/validation"
	"github.com/pkg/errors"
	redpack "wxsdk/wxpay/redpack"
)

// 红包管理
type RedPackController struct {
	base.BaseController
}

/***********发放普通红包*******************/
func (this *RedPackController) SendRedPack() {
	var (
	    curAppId  string
	)
	v := struct {
	    OpenId   string  `form:"open_id" valid:"Required"`
		SendName string  `form:"send_name" valid:"Required"`
		OrderId  string  `form:"order_id"  valid:"Required"`
		Payment  float32 `form:"payment"   valid:"Required"`
		TotalNum int     `form:"total_num" valid:"Required"`
		Wishing  string  `form:"wishing"   valid:"Required"`
		ActName  string  `form:"act_name"  valid:"Required"`
		Remark   string  `form:"remark"    valid:"Required"`
		SceneId  string  `form:"scene_id"`
		AppId    string  `form:"app_id"`
	}{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
    curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	//curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	//partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if curRoleId == 4 {
	    curAppId = this.Ctx.Input.GetData("curAppId").(string)
	} else if curRoleId == 2 {
	    if len(v.AppId) < 3 {
		    this.ServeError("app_id is invalid")
			return
		}
		curAppId = v.AppId
	}else{
	    this.ServeError("cur login role is invalid")
		return
	}
	acPay, err := isv.GetParamsByAppIdOrSubAppId(curAppId)
	if err != nil {
		err = errors.Wrap(err, "GetParamsByAppIdOrSubAppId... ")
		this.ServeError(err.Error())
		return
	}
	if curRoleId == 4 {
		acPay.AppId = "wx204da65aa7d948b0"
		acPay.SubAppId = ""
		acPay.SubMchId = ""
	} else if curRoleId == 2 {
	    if acPay.MchTypeId == 1 {
		    acPay.SubAppId = ""
		    acPay.SubMchId = ""
		} else if acPay.MchTypeId == 2 {
		  
		}else{
		    this.ServeError("支付参数商户类型无效")
		    return
		}
	}
	paymentStr := strconv.FormatInt(math.MultiTotalPriceInt(v.Payment, 100), 10)
	clientIp,  _:= helper.GetExternal()
	normalRedPackRsp, err := redpack.NormalRedPack(acPay.AppId,acPay.SubAppId,acPay.MchId,acPay.SubMchId,acPay.Appkey,
	                                               v.OrderId,v.OpenId,v.SendName,paymentStr,strconv.Itoa(v.TotalNum),clientIp,
												   v.Wishing,v.ActName,v.Remark,v.SceneId,acPay.PemCert,acPay.PemKey)		
	if err != nil {
		err = errors.Wrap(err, "NormalRedPack... ")
		this.ServeError(err.Error())
		return 
	}
	this.Data["json"] = normalRedPackRsp
	this.ServeJSON()
}

/***********发放裂变红包*******************/
func (this *RedPackController) SendGroupRedPack() {
	var (
		curOpenId  string = ""
		err        error
		acPay      *isv.AcPayParams = nil
	)
	v := struct {
	    SendName string  `form:"send_name" valid:"Required"`
		OrderId  string  `form:"order_id"  valid:"Required"`
		Payment  float32 `form:"payment"   valid:"Required"`
		TotalNum int     `form:"total_num" valid:"Required"`
		Wishing  string  `form:"wishing"   valid:"Required"`
		ActName  string  `form:"act_name"  valid:"Required"`
		Remark   string  `form:"remark"    valid:"Required"`
	}{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
    curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	//partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if curRoleId == 1 {
	    curAppId := this.Ctx.Input.GetData("curAppId").(string)
		if members, err := member.GetMemberById(curUserId); err != nil {
			this.ServeError("GetMemberById:" + err.Error())
			return
		}else{
		    curOpenId = members.OpenID
		}
		acPay, err = isv.GetParamsByAppIdOrSubAppId(curAppId)
		if err != nil {
			err = errors.Wrap(err, "GetParamsByAppIdOrSubAppId... ")
			this.ServeError(err.Error())
			return
		}
	}else{
	    this.ServeError("cur login role is invalid")
		return
	}
	paymentStr := strconv.FormatInt(math.MultiTotalPriceInt(v.Payment, 100), 10)
	clientIp,  _:= helper.GetExternal()
	groupRedPackRsp, err := redpack.GroupRedPack(acPay.AppId,acPay.SubAppId,acPay.MchId,acPay.SubMchId,acPay.Appkey,
	                                               v.OrderId,curOpenId,v.SendName,paymentStr,strconv.Itoa(v.TotalNum),clientIp,
												   v.Wishing,v.ActName,v.Remark,acPay.PemCert,acPay.PemKey)
	if err != nil {
		err = errors.Wrap(err, "GroupRedPack... ")
		this.ServeError(err.Error())
		return 
	}
	this.Data["json"] = groupRedPackRsp
	this.ServeJSON()
}
/***********查询红包记录*******************/
func (this *RedPackController) GetHBInfo() {
	var (
		err        error
		acPay      *isv.AcPayParams = nil
	)
	v := struct {
		OrderId  string  `form:"order_id"  valid:"Required"`
	}{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
    curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	//curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	//partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if curRoleId == 1 {
	    curAppId := this.Ctx.Input.GetData("curAppId").(string)
		acPay, err = isv.GetParamsByAppIdOrSubAppId(curAppId)
		if err != nil {
			err = errors.Wrap(err, "GetParamsByAppIdOrSubAppId... ")
			this.ServeError(err.Error())
			return
		}
	}else{
	    this.ServeError("cur login role is invalid")
		return
	}
	getHbInfoRsp, err := redpack.GetHbInfo(acPay.AppId,acPay.MchId,acPay.Appkey,v.OrderId,"MCHT",acPay.PemCert,acPay.PemKey)
	if err != nil {
		err = errors.Wrap(err, "GetHbInfo... ")
		this.ServeError(err.Error())
		return 
	}
	this.Data["json"] = getHbInfoRsp
	this.ServeJSON()
}

